//healnpc.txt - Acts like basic npc, but, in addition, this character will sometimes 
//heal a nerby friendly npc.
//Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//Cell 3 - Number of turns between heals.
//Cell 4 - Heal strength. Amount healed is this many d6.
//Cell 5 - Number of sheet with healing animation. Defaults to 2.
//Cell 6 - Number of state when talked to. Plays default text if 0.

begincreaturescript;

variables;

short i,target;
short last_heal;
short last_call;
short path_a_or_b = 0;
short last_walk;
short did_talk = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Sudabey");
	set_level(ME,28);
	set_boss_level(ME,2);
	set_attack_bonus(ME,10);
	set_new_abil(ME,1);
	
	last_heal = get_current_tick();
	last_call = get_current_tick();
	last_walk = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(65,7,1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		if (did_talk == 0) {
			did_talk = 1;
			talk_no_exit(50);
			}
			
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if (path_a_or_b == 0) {
		if ((tick_difference(last_walk,get_current_tick()) > 6) ||   (get_memory_cell(4) > 0)) {
			if (approach_nav_point(ME,6,2)) {
				last_walk = get_current_tick();
				path_a_or_b = 1;
				}
				else if (am_i_doing_action() == FALSE) {
					last_walk = get_current_tick();
					path_a_or_b = 1;
					}		
			}	
		}
		else if (get_memory_cell(4) == 0) {
			if (tick_difference(last_walk,get_current_tick()) > 6) {
				if (return_to_start(ME,2)) {
					last_walk = get_current_tick();
					path_a_or_b = 0;		
					}
					else if (am_i_doing_action() == FALSE) {
						last_walk = get_current_tick();
						path_a_or_b = 0;
						}
				}
			}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	// heal?
	target = get_friendly_target(ME,6,1);
	if ((my_ap() >= 4) && (target > 0) && 
	  (tick_difference(last_heal,get_current_tick()) >= 1) && (get_ran(1,0,100) < 90)) {
		if (get_friend_target() == my_number())
			print_named_str(ME,"casts a healing spell.");
			else print_named_str(ME,"heals an ally.");
		run_char_animation(2,1,95);	
		pc_heard_sound_delay(100,250);						
		last_heal = get_current_tick();
		i = get_ran(1,100,150);
		heal_char(get_friend_target(),i);
		run_sparkles_on_char(get_friend_target(),6,8,1);
		deduct_ap(4);
		end();
		}
	
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if (did_talk == 0) {
		did_talk = 1;
		talk_no_exit(50);
		}

	if (tick_difference(last_call,get_current_tick()) >= 1) {
		last_call = get_current_tick();
		i = get_ran(1,0,9);
		if ((char_ok(26 + i) == FALSE) || (can_see_char(26 + i)))
			i = get_ran(1,0,9);
		if ((char_ok(26 + i) == FALSE) || (can_see_char(26 + i)))
			i = get_ran(1,0,9);
		if ((char_ok(26 + i)) && (can_see_char(26 + i) == FALSE)) {
			print_named_str(ME,"calls for a creation to come help him.");
			set_act_at_dist(26 + i,1);
			alert_char(26 + i);
			}
		}
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(6) > 0)
		begin_talk_mode(get_memory_cell(6));
		else print_str("Talking: It doesn't respond.");
break;